module led_dynamic(//每0.5秒变化一次，显示数字123456
    input   wire        sys_clk     ,
    input   wire        sys_rst_n   ,
    input   wire [7:0]  data        ,
    input   wire        rx_vld      ,
    
    output  reg  [5:0]  sel         ,//位选信号 011111 101111 110111 111011 111101 111110
    output  reg  [7:0]  seg         //段选信号
);
parameter TIME = 26'd10000;
reg  [25: 0] cnt_fps;//数码管位选信号切换
wire         add_cnt_fps;
wire         end_cnt_fps;

reg [3:0] value;//存放当前数码管，需要显示的数字
reg [23:0] time_n;//存放时间

always @(posedge sys_clk or negedge sys_rst_n)begin
    if(!sys_rst_n)begin
        cnt_fps <= 26'd0;
    end 
    else if(add_cnt_fps)begin
        if(end_cnt_fps)begin
            cnt_fps <= 26'd0;
        end 
        else begin
            cnt_fps <= cnt_fps + 1'd1;
        end 
    end 
    else begin
        cnt_fps <= 26'd0;
    end 
end 
assign add_cnt_fps = 1'b1;
assign end_cnt_fps = add_cnt_fps && cnt_fps == TIME;

always @(posedge sys_clk or negedge sys_rst_n)begin
    if(!sys_rst_n)begin
        sel <= 6'b011_111;
    end 
    else if(end_cnt_fps)begin
        sel <= {sel[4:0], sel[5]};
    end 
    else begin
        sel <= sel;
    end 
end 

//计数程序运行后，过去的总秒数
always @(posedge sys_clk or negedge sys_rst_n) begin
    if(!sys_rst_n)begin
        time_n <= 24'hffffff;
    end
    else if(rx_vld)begin//00：00：00 清零
        case (data)
            8'd48:  
                time_n <= {time_n[19:0],4'd0};
            8'd49:  
                time_n <= {time_n[19:0],4'd1};
            8'd50:  
                time_n <= {time_n[19:0],4'd2};
            8'd51:  
                time_n <= {time_n[19:0],4'd3};
            8'd52:  
                time_n <= {time_n[19:0],4'd4};
            8'd53:  
                time_n <= {time_n[19:0],4'd5};
            8'd54:  
                time_n <= {time_n[19:0],4'd6};
            8'd55:  
                time_n <= {time_n[19:0],4'd7};
            8'd56:  
                time_n <= {time_n[19:0],4'd8};
            8'd57:  
                time_n <= {time_n[19:0],4'd9};
            default: time_n <= 24'hffffff;
        endcase
    end
    else
        time_n <= time_n;
end
//sel
always @(*)begin
    case(sel)
        6'b011_111: value = time_n[3:0]  ;
        6'b101_111: value = time_n[7:4]  ;
        6'b110_111: value = time_n[11:8] ;
        6'b111_011: value = time_n[15:12];
        6'b111_101: value = time_n[19:16];
        6'b111_110: value = time_n[23:20];
        default   : value = 4'd0         ;    
    endcase
end 

always @(*)begin 
    case (value)
        4'd0:    seg = 8'b1100_0000;
        4'd1:    seg = 8'b1111_1001;
	    4'd2:    seg = 8'b1010_0100;
	    4'd3:    seg = 8'b1011_0000;
	    4'd4:    seg = 8'b1001_1001;
	    4'd5:    seg = 8'b1001_0010;
	    4'd6:    seg = 8'b1000_0010;
	    4'd7:    seg = 8'b1111_1000;
	    4'd8:    seg = 8'b1000_0000;
	    4'd9:    seg = 8'b1001_0000;
        default :seg = 8'b00000000 ;
    endcase

end

endmodule